package com.jxwifi.cloud.quickclean.adapter;

import android.graphics.Paint;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.bean.RecordsListBean;
import com.jxwifi.cloud.quickclean.bean.StrListBean;
import com.jxwifi.cloud.quickclean.bean.ToolKitBean;

import java.util.ArrayList;
import java.util.List;

import static com.taobao.accs.ACCSManager.mContext;

public class BuyToolKitAdapter extends BaseQuickAdapter<RecordsListBean, BaseViewHolder> {

    public BuyToolKitAdapter(@LayoutRes int layoutResId) {
        super(layoutResId, null);

    }

    @Override
    protected void convert(BaseViewHolder helper, RecordsListBean item) {

        CheckBox cbx = (CheckBox) helper.getView(R.id.cb_items_child);
        cbx.setChecked(item.isCheckbox());
        Glide.with(mContext).load(item.getImgUrl()).error(R.mipmap.icon_default_head).into((ImageView) helper.getView(R.id.img_tool_kit_items_images));
        helper.setText(R.id.tv_tool_kit_items_name, item.getName());
        if (!TextUtils.isEmpty(item.getJsonStr())) {
            List<StrListBean> mList = new ArrayList<>();
            mList = JSON.parseArray(item.getJsonStr(), StrListBean.class);
            String Specifications = "";
            for (int i = 0; i < mList.size(); i++) {
                Specifications = Specifications + "" + mList.get(i).getKey() + ":" + mList.get(i).getValue();
            }
            helper.setText(R.id.tv_tool_kit_items_specifications, "规格：" + Specifications);
        } else {
            helper.setText(R.id.tv_tool_kit_items_specifications, "规格：暂无");
        }
        if (item.getTotal() > 0) {
            helper.setText(R.id.tv_buy_tool_kit_stock, "仅剩" + item.getTotal() + "" + item.getUnit());
        } else {
            helper.setText(R.id.tv_buy_tool_kit_stock, "售罄");
        }
        TextView mTvValue=helper.getView(R.id.tv_tool_kit_items_price_value);
        mTvValue.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
        if (item.getValue()!=0.0){
            if (item.getValue()>item.getDiscountValue()){
                helper.getView(R.id.tv_tool_kit_items_price_value).setVisibility(View.VISIBLE);
                helper.setText(R.id.tv_tool_kit_items_price_value, "\u00A5" + item.getValue());
            }else{
                helper.getView(R.id.tv_tool_kit_items_price_value).setVisibility(View.GONE);
                helper.setText(R.id.tv_tool_kit_items_price_value, "");
            }
        }else{
            helper.getView(R.id.tv_tool_kit_items_price_value).setVisibility(View.GONE);
            helper.setText(R.id.tv_tool_kit_items_price_value, "");
        }

        helper.setText(R.id.tv_tool_kit_items_price, "\u00A5" + item.getDiscountValue());
        helper.setText(R.id.tv_buy_tool_kit_item_num, "" + item.getToolnum());
        helper.addOnClickListener(R.id.cb_items_child)
                .addOnClickListener(R.id.tv_buy_tool_kit_item_reduce)
                .addOnClickListener(R.id.tv_buy_tool_kit_item_plus)
        .addOnClickListener(R.id.rel_buy_tool_kit);

    }
}
